const User = require('../models/user');

module.exports = (req, res, next) => {
    //检查routes/api.auth生成的 req.remoteUser; 保存到 res.locals.user
    if (req.remoteUser) {
        res.locals.user = req.remoteUser;
    }
    //检查session会话中uid
    const uid = req.session.uid; //从会话中取id
    if (!uid) return next();
    User.get(uid, (err, user) => { //从数据库取user对象
        if (err) return next(err);
        req.user = res.locals.user = user; //输出到res
        next();
    });
};